package com.xtz.controller;

import com.xtz.domain.SetMeal;
import com.xtz.domain.SetmealDishes;
import com.xtz.dto.SetMealDto;
import com.xtz.service.SetmealService;
import com.xtz.service.impl.SetmealServiceImpl;
import com.xtz.vo.Result;
import com.xtz.vo.SetmealVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * date: 2022/5/13 10:21
 *
 * @author xietianze
 */
@RestController
@RequestMapping("setmeal")
public class setmealController {

    @Autowired
    private SetmealService setmealService;
    /**
     * 添加套餐
     * @return
     */
    @PostMapping
    public Result add(@RequestBody SetMealDto dto){

        Boolean result = setmealService.addSetmeal(dto);

        return new Result(1,null,"新增成功",null);
    }

    /**
     * 分页
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result page(@RequestParam(required = false,defaultValue = "") String name,
                       @RequestParam Integer page,
                       @RequestParam Integer pageSize){
        List<SetmealVo> pageByName = setmealService.getPageByName(page, pageSize, name);

        Long aLong = setmealService.selectCountByName(name);

        SetmealVo setmealVo = new SetmealVo();
        setmealVo.setCurrent(page);
        setmealVo.setTotal(aLong);
        setmealVo.setSize(pageSize);
        setmealVo.setRecords(pageByName);

        return new Result(1,null,setmealVo,null);
    }

    /**
     * 删除套餐
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<String> ids){

        return new Result();
    }

    /**
     * 修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result setstatus(@PathVariable("status")Integer status, @RequestParam("ids") List<String> ids){
        Boolean result = setmealService.updatestatus(status, ids);

        int code = result ? 1 : 0;
        String data = result ? "成功" : "失败";

        return new Result(code, null,data, null);
    }

    /**
     * 根据id获取套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getSetmealById(@PathVariable String id){
        //查询套餐
        SetMeal setMeal = setmealService.selectsetmealById(id);
        //获取id 用于查询套餐菜品
        String id1 = setMeal.getId();
        //遍历套餐信息
        List<SetmealDishes> setmealDishes = setmealService.selectmealdishBysetmealId(id1);
        List<SetmealDishes> vo = new ArrayList<>();
        for (SetmealDishes setmealDish : setmealDishes) {
            if (!Objects.isNull(setmealDish)) {
                vo.add(setmealDish);
            }
        }
        setMeal.setSetmealDishes(vo);
        return new Result(1, "查询成功", setMeal, null);
    }

    /**
     * 修改套餐
     */
    @PutMapping
    public Result updatesetmeal(@RequestBody SetMeal setMeal){
        Boolean result = setmealService.update(setMeal);
        int code = result?1:0;
        String data = result ? "chenggong" : "shibai";
        return new Result(code,null,data,null);
    }
}
